import { throttle } from 'lodash-es'
import { userStore } from '@/stores/user'
import { ElMessage } from 'element-plus'
const injectDirective = (app: any) => {
  app.directive('focus', {
    mounted(el: HTMLElement) {
      el.focus()
    }
  })
  app.directive('throttle', {
    mounted(el: HTMLElement, binding: { value: Function }) {
      el.addEventListener('click', throttle(binding.value as any, 1000))
    }
  })
  // 复制
  app.directive('copy', {
    mounted(el: HTMLElement, binding: { value: string }) {
      el.addEventListener('click', () => {
        navigator.clipboard
          .writeText(binding.value).then(() => {
            ElMessage.success('复制成功')
          })
          .catch(() => {
            ElMessage.error('复制失败')
          })
      })
    }
  })
  app.directive('able', {
    mounted(el: HTMLElement) {
      const store = userStore()
      if (store.role !== 'manager') {
        el.disabled = true
        el.classList.add('is-disabled')
      }
    }
  })
}
export default injectDirective
